<?php
/**
 * 2016/10/22 09:59:31 
 * 张树超
 * 用户中心
 * http://yshop.huiyouzan.com/
 */
namespace Home\Controller;

use Think\Controller;

class AddressController extends HomeController
{
	public function __construct() {
		parent::__construct();
		if(empty($_SESSION['member'])) { // 判断是否登录
			//header('Location:'.U('Login/index')); // 跳转到登录页面
			//redirect(U('Login/index',array('acturl'=>urlencode($_SERVER["HTTP_REFERER"]))));
		}



		//底部菜单设置
		$menus = C("MENUS");
		$menus[3] = array(
			'href'      =>  U('/Center/index'),
			'li_class'  =>  'margin-r',
			'src'       =>  C('TMPL_PARSE_STRING')['__IMG__'].'/menu-44.png',
			'p_class'   =>  'color-menu',
			'text'      =>  '我'
		);
		C("MENUS",$menus);


	}

	/**
	 * q
	 */
	public function index() {
		$where['user_id'] = intval($_SESSION['member']['id']);
		$list = D('address')->where($where)->order('id desc')->select();
		$this->assign('list',$list);
		$this->display();
	}

	/**
	 * 修改收货人信息
	 */
	public function edit() {
		if(IS_POST) {
			$id = I('post.id','','intval');
			$data['name'] = I('post.name','','strip_tags,trim');
			if(empty($data['name'])) {
				$this->alert('收货人不能为空！');
			}
			$data['mobile'] = I('post.mobile','','strip_tags');
			if(!preg_match("/^1[3|4|5|6|7|8]\d{9}/",$data['mobile'])) {
				$this->alert('联系电话格式错误！');
			}
//			$data['email'] = I('post.email','','strip_tags,trim');
//			if(!preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$data['email'])) {
//				$this->alert('邮箱格式错误！');
//			}
			$data['address'] = I('post.address','','strip_tags,trim');
			if(empty($data)) {
				$this->alert('详细地址不能为空！');
			}
			if(I("default",0) == 1){
				$data['default'] = 1;
			}else{
				$data['default'] = 0;
			}

			$address = D('address');

			if(!empty($id)) { // 判断提交是修改还是添加
				$msg = $address->where(array('id'=>$id))->save($data);
			} else {
				$data['user_id'] = intval($_SESSION['member']['id']);
				$msg = $address->add($data);
			}

			if(empty($msg)) {
//				$this->alert('操作失败！ ');
				$this->alert('操作成功！',!empty($url)?urldecode($url):U('Address/index'));
			} else {
				$url = I("url",'');
				$this->alert('操作成功！',!empty($url)?urldecode($url):U('Address/index'));
			}
		}

		$id = I('get.id','','intval');
		if(!empty($id)) { // 判断是修改还是添加
			$info = D('address')->find($id); // 获取地址信息
//			dump($info);
			trace($info,'info');
			$this->assign('info',$info);
		}
		$this->display();
	}
	
	//ajax 更新地址
	public function update(){
		// 验证
		if(empty($_POST['id']) || empty(session("member.id"))){
			$this->ajaxReturn(array('code'=>0,'msg'=>'无I或UID '));
		}
		// 查询条件
		$map['id'] = $_POST['id'];
		$map['user_id'] = session("member.id");

		//新数据
		if($_POST['default'] == 'true'){
			$data['default'] = 1;
		}else{
			$data['default'] = 0;
		}

		$Address = D("Address");
		$Address->where(array('user_id='.session("member.id")))->save(array('default'=>0));
		if($Address->where($map)->save($data)){
			$this->ajaxReturn(array('code'=>1));
		}else{
			$this->ajaxReturn(array('code'=>0,'msg'=>'保存失败 '.$Address->getError()));
		}
	}

//	订单选择
	public function select(){
		$where['user_id'] = intval($_SESSION['member']['id']);
		$lists = D('address')->where($where)->order('id desc')->select();
		$this->assign('lists',$lists);
		trace($lists,'lists');
		$this->display();
	}


	/**
	 * 删除收货地址
	 */
	public function delete() {
		$id = I('get.id','','intval');
		$msg = D('address')->delete($id);
		if(!empty($msg)) {
			$this->alert('删除成功！',U('Address/index'));
		} else {
			$this->alert('删除失败！');
		}
	}

	/**
	 * 成功跳转
	 */
	public function alert($msg,$url) {
		$this->assign('info',array('msg'=>$msg,'url'=>$url));
		$this->display('Notice/location');
		die();
	}

}
